package com.heima.article.service.iml;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.AuthorMapper;
import com.heima.article.service.ApAuthorService;
import com.heima.common.exception.CustException;
import com.heima.common.exception.CustomException;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;

@Service
public class ApAuthorServiceImpl extends ServiceImpl<AuthorMapper, ApAuthor> implements ApAuthorService {
    /*
    *根据ApUser id查询作者信息
    * */
    @Override
    public ResponseResult findByUserId(Integer userId) {
        //1.参数校验
        if (userId == null){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.查询作者信息
        ApAuthor apAuthor = getOne(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getUserId, userId));
        return ResponseResult.okResult(apAuthor);
    }

    //保存作者信息
    @Override
    public ResponseResult<ApAuthor> saveApAuthor(ApAuthor apAuthor) {
        //1.参数校验
        if (apAuthor == null){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断在作者库中有没有
        boolean save = this.save(apAuthor);
        if (save){
            return ResponseResult.okResult(apAuthor);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"信息保存出错");

    }
}
